package com.example.admintor.threehome.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.ImageView;

import com.example.admintor.threehome.data.Datas;
import com.example.admintor.threehome.data.ReciveBean;
import com.example.admintor.threehome.data.ReciveDateHander;
import com.example.admintor.threehome.fragment.FourFragment;
import com.example.admintor.threehome.fragment.OneFragment;
import com.example.admintor.threehome.R;
import com.example.admintor.threehome.SysApplication;
import com.example.admintor.threehome.fragment.ThreeFragment;
import com.example.admintor.threehome.fragment.TwoFragment;
import com.example.wzsdk.login.Landing;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends Activity {
    OneFragment oneFragment;
    TwoFragment twoFragment;
    ThreeFragment threeFragment;
    FourFragment fourFragment;

    private Landing wzoneLanding;
    private ReciveDateHander reciveDateHander;

    @BindView(R.id.hj_img)
    ImageView hj_img;

    @BindView(R.id.kz_img)
    ImageView kz_img;

    @BindView(R.id.yp_img)
    ImageView yp_img;

    @BindView(R.id.af_img)
    ImageView af_img;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    wzoneLanding.AttenVarsByUiid(Datas.HT_Uiid);
                    handler.sendEmptyMessageDelayed(0x456, 2000);
                    break;
                case 0x456:
                    wzoneLanding.AttenVarsByUiid(Datas.Ill_Uiid);
                    handler.sendEmptyMessageDelayed(0x789, 2000);
                    break;
                case 0x789:
                    wzoneLanding.AttenVarsByUiid(Datas.Sm_Uiid);
                    handler.sendEmptyMessageDelayed(0x112, 2000);
                    break;
                case 0x112:
                    wzoneLanding.AttenVarsByUiid(Datas.RT_Uiid);
                    handler.sendEmptyMessageDelayed(0x223, 2000);
                    break;
                case 0x223:
                    wzoneLanding.AttenDevByUiid(Datas.RFID_Uiid);
                    handler.sendEmptyMessageDelayed(0x965, 2000);
                    break;
                case 0x965:
                    wzoneLanding.AttenVarsByUiid(Datas.Sound_Uiid);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initWzoneSDK();
        oneFragment = new OneFragment();
        twoFragment = new TwoFragment();
        threeFragment = new ThreeFragment();
        fourFragment = new FourFragment();
        getFragmentManager().beginTransaction().add(R.id.parent, fourFragment).commit();
    }

    private void initWzoneSDK() {
        reciveDateHander = (ReciveDateHander) getIntent().getSerializableExtra("DateHandler");
        wzoneLanding = reciveDateHander.getWzoneLanding();
    }

    @Override
    protected void onStart() {
        super.onStart();
        handler.sendEmptyMessage(0x123);
    }


    @OnClick(R.id.hj_img)
    public void OnClickHj() {
        kz_img.setBackgroundResource(R.drawable.kz_up);
        yp_img.setBackgroundResource(R.drawable.yp_up);
        af_img.setBackgroundResource(R.drawable.an_up);
        kz_img.clearAnimation();
        yp_img.clearAnimation();
        af_img.clearAnimation();
        getFragmentManager().beginTransaction().replace(R.id.parent, oneFragment).commit();
    }

    @OnClick(R.id.kz_img)
    public void OnClickKz() {
        hj_img.setBackgroundResource(R.drawable.hj_up);
        yp_img.setBackgroundResource(R.drawable.yp_up);
        af_img.setBackgroundResource(R.drawable.an_up);
        hj_img.clearAnimation();
        yp_img.clearAnimation();
        af_img.clearAnimation();
        getFragmentManager().beginTransaction().replace(R.id.parent, fourFragment).commit();
    }

    @OnClick(R.id.yp_img)
    public void OnClickYp() {
        hj_img.setBackgroundResource(R.drawable.hj_up);
        kz_img.setBackgroundResource(R.drawable.kz_up);
        af_img.setBackgroundResource(R.drawable.an_up);
        hj_img.clearAnimation();
        kz_img.clearAnimation();
        af_img.clearAnimation();
        getFragmentManager().beginTransaction().replace(R.id.parent, twoFragment).commit();
    }

    @OnClick(R.id.af_img)
    public void OnClickAf() {
        hj_img.setBackgroundResource(R.drawable.hj_up);
        yp_img.setBackgroundResource(R.drawable.yp_up);
        kz_img.setBackgroundResource(R.drawable.kz_up);
        hj_img.clearAnimation();
        kz_img.clearAnimation();
        yp_img.clearAnimation();
        getFragmentManager().beginTransaction().replace(R.id.parent, threeFragment).commit();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(this).setTitle("系统提示").setMessage("是否要退出？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    wzoneLanding.exitLogin();
                    SysApplication.getInstance().exit();
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).show();
        }
        return super.onKeyDown(keyCode, event);
    }

    public ReciveDateHander getReciveDateHander() {
        return reciveDateHander;
    }
}
